﻿
Partial Class WebForm_ManageProduct_ImportProductData
    Inherits System.Web.UI.UserControl
    Protected Sub btnImportProductData_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImportProductData.PreRender
        ScriptManager.GetCurrent(Me.Page).RegisterPostBackControl(btnImportProductData)
    End Sub

    Protected Sub btnImportProductData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImportProductData.Click
        If Not FileUpload1.HasFile Then
            MyObj.ShowMessage("no file")
            Return
        ElseIf FileUpload1.PostedFile.ContentType.ToString() <> "application/vnd.ms-excel" AndAlso FileUpload1.PostedFile.ContentType.ToString() <> "application/octet-stream" Then
            MyObj.ShowMessage("Import failed!!Please select the file extension (xls, csv) file!!")
            Return
        End If
        '上傳暫存檔
        Dim clsRandom As New clsRandom()
        Dim newName As String = Server.MapPath("~/") & "/tmp/" & clsRandom.Generate()
        FileUpload1.PostedFile.SaveAs(newName)

        Dim ExcelImportCheck As New ExcelImportCheck()
        Dim dt As New Data.DataTable

        '檢查工作表名稱
        If ExcelImportCheck.LoadData(dt, newName, "ProductData", IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper()) = False Then
            Return
        Else
            Me.ImportProductData = dt
        End If

        If Me.ImportProductData.Rows.Count > 0 Then
            Dim sql As New MyStringBuilder
            Dim dat As New clsData
            sql.Length = 0
            sql.AppendFormat("truncate table ProductData ")

            For i As Integer = 0 To Me.ImportProductData.Rows.Count - 1
                sql.AppendFormat(";insert into ProductData(SapCode,ProdHierarchy,Description,MMPP) ")
                sql.AppendFormat("select '{0}' ", Me.ImportProductData.Rows(i)("SapCode"))
                sql.AppendFormat(",'{0}' ", Me.ImportProductData.Rows(i)("ProdHierarchy"))
                sql.AppendFormat(",'{0}' ", Me.ImportProductData.Rows(i)("Description"))
                sql.AppendFormat(",'{0}' ", Me.ImportProductData.Rows(i)("MMPP"))

                dat.AccData(sql.ToString())
            Next
            MyObj.ShowMessage("Import successful!!!")
        Else
            MyObj.ShowMessage("No Data is available for Import!!")
        End If
    End Sub
    Property ImportProductData() As Data.DataTable
        Get
            Return ViewState("ImportProductData")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("ImportProductData") = value
        End Set
    End Property
End Class
